<script setup>
import {ref, onMounted} from 'vue'
import {ElCard, ElTable, ElTableColumn, ElMessage,ElMessageBox} from 'element-plus'
import {
  getConsumablesStoreList,
  addConsumablesStore,
  deleteConsumablesStore,
  getConsumablesStoreDetail,
  updateConsumablesStore
} from "@/api/ConsumablesStore.js"

import {
  Search,
  Plus,
  Refresh, Edit, Delete
} from '@element-plus/icons-vue'


// 弹框状态
const formVisible = ref(false)
const title = ref('')

// 仓库表单数据
const storeForm = ref({
  name: '',
  remark: ''
})

// 是否为编辑状态
const isEditMode = ref(false)

// 当前编辑的仓库ID
const currentStoreId = ref(null)

// 表单验证规则
const rules = {
  name: [
    {required: true, message: '请输入仓库名称', trigger: 'blur'}
  ]
}

//仓库列表
const storeList = ref([])
// 获取耗材仓库列表
const getConsumablesStoreListData = (data) => {
  getConsumablesStoreList(data).then(res => {
    if (res.code === 200) {
      storeList.value = res.data
    }
  })
}

// 提交仓库表单
const handleSubmit = () => {
  // 检查仓库名称是否为空
  if (!storeForm.value.name) {
    ElMessage.warning('请输入仓库名称');
    return;
  }
  const apiMethod = isEditMode.value
      ? updateConsumablesStore
      : addConsumablesStore;

  const requestData = {
    name: storeForm.value.name,
    remark: storeForm.value.remark
  };

  if (isEditMode.value && currentStoreId.value) {
    requestData.id = currentStoreId.value;
  }

  apiMethod(requestData).then(res => {
    if (res.code === 200) {
      ElMessage.success(`${isEditMode.value ? '更新' : '新增'}成功`);
      formVisible.value = false;
      resetForm();
      getConsumablesStoreListData();
    }
  });
}

// 重置表单
const resetForm = () => {
  isEditMode.value = false;
  currentStoreId.value = null;
  storeForm.value = {name: '', remark: ''};
}

// 新增耗材仓库
const addConsumablesStoreData = () => {
  resetForm();
  formVisible.value = true
  title.value = '新增仓库'
}

// 删除耗材仓库
const deleteConsumablesStoreData = (row) => {
  //console.log(row, "删除")
  ElMessageBox.confirm( '确定删除该耗材仓库信息吗?',  '系统警告',    {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteConsumablesStore(row.id).then(res => {
      if (res.code === 200) {
        ElMessage.success('删除成功')
        getConsumablesStoreListData()
      }
    }).catch(err => {
      ElMessage.error('删除失败')
    })
  }).catch(() => {
    console.log('取消删除')
  })

}

// 编辑耗材仓库
const editConsumablesStoreData = (row) => {
  title.value = '编辑仓库'
  // 根据id获取仓库详情
  getConsumablesStoreDetail(row.id).then(res => {
    if (res.code === 200) {
      isEditMode.value = true;
      currentStoreId.value = row.id;
      storeForm.value = {
        name: res.data.name,
        remark: res.data.remark
      }
      formVisible.value = true
    }
  })
}
// 搜索框绑定的仓库名称
const queryForm = ref({
  name: ''
})
// 搜索按钮点击事件
const handleSearch = () => {
  // 判断是否为空
  if (!queryForm.value.name) {
    ElMessage.warning('请输入仓库名称');
    return;
  }
  getConsumablesStoreListData(queryForm.value)
}
// 重置搜索条件
const handleReset = () => {
  queryForm.value.name = ''
  getConsumablesStoreListData()
}

onMounted(() => {
  getConsumablesStoreListData()
})
</script>


<template>

  <!-- 新增搜索框 -->
  <el-form :inline="true" :model="queryForm" class="demo-form-inline" style="margin-top: 10px;">
    <el-form-item>
      <el-space>
        <el-input v-model="queryForm.name" placeholder="请输入仓库名称" clearable style="width: 300px"
                  :prefix-icon="Search">
          <template #append>
            <el-button type="primary" :icon="Search" :style="$globalAttribute.primaryButton" @click="handleSearch">
              搜索
            </el-button>
          </template>
        </el-input>
        <el-button type="primary" plain :icon="Refresh" @click="handleReset">
          重置
        </el-button>
        <el-button plain type="primary" :icon="Plus" @click="addConsumablesStoreData">
          新增
        </el-button>
      </el-space>
    </el-form-item>
  </el-form>


  <el-dialog v-model="formVisible" :title="title" width="30%">
    <el-form :model="storeForm" :rules="rules" label-width="100px">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="storeForm.name" maxlength="30" show-word-limit clearable placeholder="请输入仓库名称"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="storeForm.remark" maxlength="50" show-word-limit clearable
                  placeholder="请输入备注"/>
      </el-form-item>
      <div class="dialog-footer">
        <el-button @click="formVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </el-form>
  </el-dialog>

  <el-table
      :data="storeList"
      table-layout="auto"
      height="80%"
      style="margin-top: 10px;"
  >
    <el-table-column prop="name" label="仓库名称" align="left"/>
    <!--      <el-table-column prop="createTime" label="创建时间" align="center"/>-->
    <!--      <el-table-column prop="remark" label="备注" align="center">-->
    <!--          <template #default="scope">-->
    <!--            <el-tooltip :content="scope.row.remark" placement="top" :disabled="!scope.row.remark || scope.row.remark.length <= 10">-->
    <!--              <span class="table-cell-truncate">{{ scope.row.remark }}</span>-->
    <!--            </el-tooltip>-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <el-table-column label="操作" align="center" width="200">
      <template #default="scope">
        <el-space>
          <el-link type="primary" link :icon="Edit" @click="editConsumablesStoreData(scope.row)">
            修改
          </el-link>
          <el-link type="danger" link :icon="Delete" @click="deleteConsumablesStoreData(scope.row)">
            删除
          </el-link>
        </el-space>
      </template>
    </el-table-column>
  </el-table>
</template>

<style scoped lang='less'>
.mb-4 {
  margin-bottom: 1rem;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.table-cell-truncate {
  display: inline-block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px; /* 根据实际情况调整 */
}
</style>